package com.example.yuejian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.yuejian.R;
import com.example.yuejian.fragment.club.ClubCourseFragment;
import com.example.yuejian.fragment.club.ClubIntroFragment;
import com.example.yuejian.fragment.club.ClubVideoFragment;
import com.squareup.picasso.Picasso;


public class ClubDetail extends AppCompatActivity implements View.OnClickListener
{
    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;

    private LinearLayout tab_intro;
    private LinearLayout tab_course;
    private LinearLayout tab_video;

    private TextView intro,course,video,title;
    private ImageView img,back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_detail);
        initView();
        selectEvents();
        setSelect(0);

    }

    private void initView(){
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        img = (ImageView)findViewById(R.id.img);
        back = (ImageView)findViewById(R.id.back);
        Picasso.with(this).load(bundle.getString("pic")).into(img);

        tab_intro = (LinearLayout)findViewById(R.id.tab_intro);
        tab_course = (LinearLayout)findViewById(R.id.tab_course);
        tab_video = (LinearLayout)findViewById(R.id.tab_video);
        intro = (TextView) findViewById(R.id.intro);
        course = (TextView) findViewById(R.id.course);
        video = (TextView) findViewById(R.id.video);
        title = (TextView) findViewById(R.id.title);
        title.setText(bundle.getString("clubname"));
    }
    private void selectEvents(){
        tab_intro.setOnClickListener(this);
        tab_course.setOnClickListener(this);
        tab_video.setOnClickListener(this);
        back.setOnClickListener(this);
    }
    private void setSelect(int i)
    {
        FragmentManager fm = ClubDetail.this.getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i) {

            case 0:
                if (mTab01 == null) {
                    mTab01 = new ClubIntroFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else {
                    transaction.show(mTab01);
                }
                intro.setTextColor(ClubDetail.this.getResources().getColor(R.color.white));
                tab_intro.setBackgroundColor(ClubDetail.this.getResources().getColor(R.color.bg_Gray));

                break;
            case 1:
                if (mTab02 == null) {
                    mTab02 = new ClubCourseFragment();
                    transaction.add(R.id.id_content, mTab02);
                } else {
                    transaction.show(mTab02);

                }
                course.setTextColor(ClubDetail.this.getResources().getColor(R.color.white));
                tab_course.setBackgroundColor(ClubDetail.this.getResources().getColor(R.color.bg_Gray));

                //tv1.setBackgroundColor(Color.BLUE);
                break;
            case 2:
                if (mTab03 == null) {
                    mTab03 = new ClubVideoFragment();
                    transaction.add(R.id.id_content, mTab03);
                } else {
                    transaction.show(mTab03);
                }
               video.setTextColor(ClubDetail.this.getResources().getColor(R.color.white));
               tab_video.setBackgroundColor(ClubDetail.this.getResources().getColor(R.color.bg_Gray));

                break;

            default:
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }
    }

    private void resetImgs()
    {
        tab_intro.setBackgroundColor(ClubDetail.this.getResources().getColor(R.color.white));
        tab_course.setBackgroundColor(ClubDetail.this.getResources().getColor(R.color.white));
        tab_video.setBackgroundColor(ClubDetail.this.getResources().getColor(R.color.white));


        intro.setTextColor(ClubDetail.this.getResources().getColor(R.color.bg_Gray));
        course.setTextColor(ClubDetail.this.getResources().getColor(R.color.bg_Gray));
        video.setTextColor(ClubDetail.this.getResources().getColor(R.color.bg_Gray));
    }

    @Override
    public void onClick(View view) {
        resetImgs();
        switch (view.getId()){
            case R.id.tab_intro:
                setSelect(0);
                break;
            case R.id.tab_course:
                setSelect(1);
                break;
            case R.id.tab_video:
                setSelect(2);
                break;
            case R.id.back:
               onBackPressed();
                break;
            default:
                break;
        }

    }
}